package com.cn.wanxi.util;

import com.cn.wanxi.dao.NavDao;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class IO {
    //    目的：不能让别人实例化当前对象
    private static File file = null;


    private IO() {

    }

    /**
     * 创建文件和文件夹
     */
    private static void creatNote(String path, String fileName) {
        File directory = new File(path);
        directory.isDirectory();//判断是否是文件夹
        if (!directory.exists()) {
            directory.mkdirs();
        }
        File file = new File(directory, fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        如果变量是静态static，要调用，类名.变量名
//        如果是普通变量，则用this.变量名
        IO.file = file;
    }

    public static void writeNote(List<String> list, String fileName) {
        creatNote(Path.PATH.getPath(), fileName);
        try {
            OutputStream outputStream = new FileOutputStream(file);
            for (int i = 0; i < list.size(); i++) {
                outputStream.write(list.get(i).getBytes("UTF-8"));
                outputStream.write(Path.SPLIT.getPath().getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取数据
     */
    public static String readNote() {
        StringBuilder data = new StringBuilder();
        try {
//            InputStream inputStream = new FileInputStream(file);
//            int result = inputStream.read();
//            while (result != -1) {
//                System.out.print((char) result);
//                result = inputStream.read();
//
//            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
//            Reader reader = new FileReader(new FileInputStream(file), "UTF-8");
            int result = reader.read();
            while (result != -1) {
//                System.out.print((char) result);
                data.append((char) result);
                result = reader.read();
            }
//            String和StringBuilder和StringBuffer的区别？


//            System.out.println(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data.toString();
    }

}
